package my.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import my.jdbc.DbConUtil;
import my.jdbc.JdbcTemplate;
import my.job.TimeTaskJobUtil;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bean.AllSyncLogsBean;
import bean.PageParamBean;

@Controller
@RequestMapping("sync")
public class SyncJhysAction {

	@Resource(name = "timeTaskSyncJob")
	private TimeTaskJobUtil timeTaskJobUtil;

	/**
	 * 手工同步进货验收数据
	 * 
	 * @param time
	 */
	@RequestMapping("manualSync")
	@ResponseBody
	public void manualSyncJhys(String time) {
		timeTaskJobUtil.syncJhys(time);
	}

	/**
	 * 手工同步站级数据( 同步进货验收,销售数据,时点库存数据,整点库存数据)
	 * 
	 * @param btime
	 * @param etime
	 * @return
	 */
	@RequestMapping("syncAll")
	@ResponseBody
	public void syncAll(String btime, String etime) {
		timeTaskJobUtil.syncStationData(btime, etime);
	}

	@RequestMapping("allsynclogs")
	@ResponseBody
	public Object syncLogsAction(PageParamBean page, String selectType, String btime, String etime, String syncStatus) {
		Map<String, Object> pageMap = new HashMap<String, Object>();
		if (StringUtils.isBlank(selectType)) {
			pageMap.put("total", 0);
			pageMap.put("rows", new ArrayList<AllSyncLogsBean>());
			return pageMap;
		}
		String countSql = " SELECT COUNT(1) FROM all_sync_logs WHERE 1=1 ";
		String sql = " SELECT * FROM ( SELECT row_number() OVER (ORDER BY insertTime DESC) rowId,stationCode,stationName,message,insertTime,ip FROM all_sync_logs WHERE 1=1 ";
		if (StringUtils.isNotBlank(btime) && StringUtils.isNotBlank(etime)) {
			countSql += " AND CONVERT(VARCHAR, insertTime, 20) >= '" + btime + "' AND  CONVERT(VARCHAR, insertTime, 20) <= '" + etime + "' ";
			sql += "  AND CONVERT(VARCHAR, insertTime, 20) >= '" + btime + "' AND  CONVERT(VARCHAR, insertTime, 20) <= '" + etime + "'  ";
		}

		if (StringUtils.isNotBlank(syncStatus)) {
			if (syncStatus.equals("1")) {
				countSql += " AND message LIKE '%进货验收数据%' ";
				sql += " AND message LIKE '%进货验收数据%' ";
			} else {
				countSql += " AND message LIKE '%IP地址%' ";
				sql += " AND message LIKE '%IP地址%' ";
			}
		}

		sql += " ) AS t WHERE t.rowId BETWEEN " + (page.getOffset() + 1) + "  AND " + page.getPage() * page.getPagesize() + " ";
		List<AllSyncLogsBean> jhysLogList = JdbcTemplate.queryListInfoByFields(DbConUtil.get162testCon(), sql, AllSyncLogsBean.class, null, DbConUtil.TYPE_NAME_162TEST);
		Object[] count = JdbcTemplate.queryFieldInfoByFields(DbConUtil.get162testCon(), countSql, null, DbConUtil.TYPE_NAME_162TEST);
		pageMap.put("total", count[0]);
		pageMap.put("rows", jhysLogList);
		return pageMap;
	}

	/**
	 * 同步实际发油数据
	 */
	@RequestMapping("syncSjfy")
	@ResponseBody
	public void syncSjfy(String time) {

		timeTaskJobUtil.syncSjfyData(time);
	}

	public static void main(String[] args) {
		// SyncJhysAction s = new SyncJhysAction();
	}

	public void setTimeTaskJobUtil(TimeTaskJobUtil timeTaskJobUtil) {
		this.timeTaskJobUtil = timeTaskJobUtil;
	}

}
